package es.ats.codington.festival.daos;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.exceptions.DAOException;
import es.ats.codington.festival.interfaces.daos.IEventDAO;
import es.ats.codington.festival.utils.FERSDataConnection;
import es.ats.codington.festival.utils.FERSDbQuery;

public class EventDAOImp implements IEventDAO{
	private FERSDataConnection con;
	private FERSDbQuery query;
	
	public EventDAOImp() {
		con = new FERSDataConnection();
		query = new FERSDbQuery();
    }
    	
    public FERSDataConnection getCon() {
		return con;
	}

	public void setCon(FERSDataConnection con) {
		this.con = con;
	}

	public FERSDbQuery getQuery() {
		return query;
	}

	public void setQuery(FERSDbQuery query) {
		this.query = query;
	}

	@Override
    public Event getEventById(int eventId) throws DAOException{
    	Event e=new Event();
    	try{
	    	PreparedStatement pStmt = con.prepareStatement(query.getEventById());
	    	pStmt.setInt(1,eventId);
	    	ResultSet rs = pStmt.executeQuery();
			while (rs.next()) {
				e.setEventId(Integer.parseInt(rs.getString("eventId")));
				e.setName(rs.getString("name"));
				e.setDescription(rs.getString("description"));
				e.setDuration(Integer.parseInt(rs.getString("duration")));
				Place p=new Place();
				p.setPlaceId(Integer.parseInt(rs.getString("placeId")));
				e.setPlace(p);
				e.setPrice(Float.parseFloat(rs.getString("price")));
				e.setSeatsAvailable(Integer.parseInt(rs.getString("seats_available")));
				e.setStart(rs.getTimestamp("start"));
			}
			return e;
		} catch (Exception ex) {
			throw new DAOException("DB getEventById failure:"+ex.getMessage(), ex);
		}
    }

    @Override
    public int insertEvent(Event event) throws DAOException{
    	try{
	    	PreparedStatement pStmt = con.prepareStatement(query.insertEvent(event));
	    	pStmt.setString(1,event.getName());
	    	pStmt.setString(2,event.getDescription());
	    	pStmt.setString(3,new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(event.getStart()));
	    	pStmt.setInt(4,event.getDuration());
	    	pStmt.setFloat(5,event.getPrice());
	    	pStmt.setInt(6,event.getSeatsAvailable());
	    	pStmt.setInt(7,event.getPlace().getPlaceId());
	    	int affectedRows=pStmt.executeUpdate();
			return affectedRows;
		} catch (Exception ex) {
			throw new DAOException("DB getEventById failure:"+ex.getMessage(), ex);
		}
    }

    @Override
    public int deleteEventById(int eventId) throws DAOException{
    	try{
	    	PreparedStatement pStmt = con.prepareStatement(query.deleteEventById());
	    	pStmt.setInt(1,eventId);
	    	int affectedRows=pStmt.executeUpdate();
			return affectedRows;
		} catch (Exception ex) {
			throw new DAOException("DB deleteEventById failure:"+ex.getMessage(), ex);
		}
    }

	@Override
	public int checkEventSeatsAvailableById(int eventId) throws DAOException{
		int seats=-1;
		try{
	    	PreparedStatement pStmt = con.prepareStatement(query.checkEventSeatsAvailableById(eventId));
	    	pStmt.setInt(1,eventId);
	    	ResultSet rs = pStmt.executeQuery();
			if (rs.next()) {
				seats=Integer.parseInt(rs.getString(1));
			}
			return seats;
		} catch (Exception ex) {
			throw new DAOException("DB checkEventSeatsAvailableById failure:"+ex.getMessage(), ex);
		}
	}

	@Override
	public String checkEventNameById(int eventId) throws DAOException{
		String name=new String();
		try{
	    	PreparedStatement pStmt = con.prepareStatement(query.checkEventNameById(eventId));
	    	pStmt.setInt(1,eventId);
	    	ResultSet rs = pStmt.executeQuery();
			if (rs.next()) {
				name=rs.getString(1);
			}
			return name;
		} catch (Exception ex) {
			throw new DAOException("DB checkEventNameById failure:"+ex.getMessage(), ex);
		}
	}

	@Override
	public String checkEventDescriptionById(int eventId) throws DAOException{
		String name=new String();
		try{
	    	PreparedStatement pStmt = con.prepareStatement(query.checkEventDescriptionById(eventId));
	    	pStmt.setInt(1,eventId);
	    	ResultSet rs = pStmt.executeQuery();
			if (rs.next()) {
				name=rs.getString(1);
			}
			return name;
		} catch (Exception ex) {
			throw new DAOException("DB checkEventDescriptionById failure:"+ex.getMessage(), ex);
		}
	}

	@Override
	public Date checkEventStartById(int eventId) throws DAOException{
		Date d=new Date();
		try{
	    	PreparedStatement pStmt = con.prepareStatement(query.checkEventStartById(eventId));
	    	pStmt.setInt(1,eventId);
	    	ResultSet rs = pStmt.executeQuery();
			if (rs.next()) {
				d=rs.getTimestamp(1);
			}
			return d;
		} catch (Exception ex) {
			throw new DAOException("DB checkEventStartById failure:"+ex.getMessage(), ex);
		}
	}

	@Override
	public int checkEventDurationById(int eventId) throws DAOException{
		int duration=-1;
		try{
	    	PreparedStatement pStmt = con.prepareStatement(query.checkEventDurationById(eventId));
	    	pStmt.setInt(1,eventId);
	    	ResultSet rs = pStmt.executeQuery();
			if (rs.next()) {
				duration=Integer.parseInt(rs.getString(1));
			}
			return duration;
		} catch (Exception ex) {
			throw new DAOException("DB checkEventDurationById failure:"+ex.getMessage(), ex);
		}
	}

	@Override
	public Float checkEventPriceById(int eventId) throws DAOException{
		float price=-1;
		try{
	    	PreparedStatement pStmt = con.prepareStatement(query.checkEventPriceById(eventId));
	    	pStmt.setInt(1,eventId);
	    	ResultSet rs = pStmt.executeQuery();
			if (rs.next()) {
				price=Float.parseFloat(rs.getString(1));
			}
			return price;
		} catch (Exception ex) {
			throw new DAOException("DB checkEventPriceById failure:"+ex.getMessage(), ex);
		}
	}

	@Override
	public int checkEventPlaceById(int eventId) throws DAOException{
		int place=-1;
		try{
	    	PreparedStatement pStmt = con.prepareStatement(query.checkEventPlaceById(eventId));
	    	pStmt.setInt(1,eventId);
	    	ResultSet rs = pStmt.executeQuery();
			if (rs.next()) {
				place=Integer.parseInt(rs.getString(1));
			}
			return place;
		} catch (Exception ex) {
			throw new DAOException("DB checkEventPlaceById failure:"+ex.getMessage(), ex);
		}
	}

	@Override
	public int updateEventNameById(int eventId, String name) throws DAOException{
		try{
	    	PreparedStatement pStmt = con.prepareStatement(query.updateEventNameById());
	    	pStmt.setString(1,name);
	    	pStmt.setInt(2,eventId);
	    	int affectedRows=pStmt.executeUpdate();
			return affectedRows;
		} catch (Exception ex) {
			throw new DAOException("DB updateEventNameById failure:"+ex.getMessage(), ex);
		}
	}

	@Override
	public int updateEventDescriptionById(int eventId, String description) throws DAOException{
		try{
	    	PreparedStatement pStmt = con.prepareStatement(query.updateEventDescriptionById());
	    	pStmt.setString(1,description);
	    	pStmt.setInt(2,eventId);
	    	int affectedRows=pStmt.executeUpdate();
			return affectedRows;
		} catch (Exception ex) {
			throw new DAOException("DB updateEventDescriptionById failure:"+ex.getMessage(), ex);
		}
	}

	@Override
	public int updateEventStartById(int eventId, Date start) throws DAOException{
		try{
	    	PreparedStatement pStmt = con.prepareStatement(query.updateEventStartById());
	    	pStmt.setString(1,new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(start));
	    	pStmt.setInt(2,eventId);
	    	int affectedRows=pStmt.executeUpdate();
			return affectedRows;
		} catch (Exception ex) {
			throw new DAOException("DB updateEventStartById failure:"+ex.getMessage(), ex);
		}
	}

	@Override
	public int updateEventDurationById(int eventId, int duration) throws DAOException{
		try{
	    	PreparedStatement pStmt = con.prepareStatement(query.updateEventDurationById());
	    	pStmt.setInt(1,duration);
	    	pStmt.setInt(2,eventId);
	    	int affectedRows=pStmt.executeUpdate();
			return affectedRows;
		} catch (Exception ex) {
			throw new DAOException("DB updateEventDurationById failure:"+ex.getMessage(), ex);
		}
	}

	@Override
	public int updateEventPriceById(int eventId, Float price) throws DAOException{
		try{
	    	PreparedStatement pStmt = con.prepareStatement(query.updateEventPriceById());
	    	pStmt.setFloat(1,price);
	    	pStmt.setInt(2,eventId);
	    	int affectedRows=pStmt.executeUpdate();
			return affectedRows;
		} catch (Exception ex) {
			throw new DAOException("DB updateEventPriceById failure:"+ex.getMessage(), ex);
		}
	}

	@Override
	public int updateEventPlaceById(int eventId, int placeId) throws DAOException{
		try{
	    	PreparedStatement pStmt = con.prepareStatement(query.updateEventPlaceById());
	    	pStmt.setInt(1,placeId);
	    	pStmt.setInt(2,eventId);
	    	int affectedRows=pStmt.executeUpdate();
			return affectedRows;
		} catch (Exception ex) {
			throw new DAOException("DB updateEventPlaceById failure:"+ex.getMessage(), ex);
		}
	}

	@Override
	public List<Event> getAllEvents() throws DAOException {
		List<Event> events = new ArrayList<Event>();
		
		try{
	    	PreparedStatement pStmt = con.prepareStatement(query.getAllEvents());
	    	ResultSet rs = pStmt.executeQuery();
	    	while(rs.next()) {	      
			    Event e = new Event();
			
			    e.setEventId(Integer.parseInt(rs.getString("eventId")));
				e.setName(rs.getString("name"));
				e.setDescription(rs.getString("description"));
				e.setDuration(Integer.parseInt(rs.getString("duration")));
				Place p=new Place();
				p.setPlaceId(Integer.parseInt(rs.getString("placeId")));
				e.setPlace(p);
				e.setPrice(Float.parseFloat(rs.getString("price")));
				e.setSeatsAvailable(Integer.parseInt(rs.getString("seats_available")));
				e.setStart(rs.getTimestamp("start"));
			 
			    events.add(e);
			    
			}
			return events;
		} catch (Exception ex) {
			throw new DAOException("DB getAllEvents failure:"+ex.getMessage(), ex);
		}
	}

	@Override
	public List<Event> getEventsByDescription(String description)
			throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}
	
	 @Override
	    public List<Event> getEventsByName(String eventName) throws DAOException{
			return null;
	    	//Implementation goes here
	    }
	@Override
	public List<Event> getEventsByPlaceId(int placeId) throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Event> getEventsByPlaceType(int type) throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Event> getBusinessEvents() throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Event> getMarketEvents() throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Event> getMuseumEvents() throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Event> getParkEvents() throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Event> getStadiumEvents() throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Event> getTheaterEvents() throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Event> getTouristEvents() throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Event> getZooEvents() throws DAOException {
		// TODO Auto-generated method stub
		return null;
	}
    
}